package light.head.dao.oa;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import light.head.constant.Parm;
import light.head.constant.Sys;
import light.head.enm.RESULT;
import light.head.framework.base.BaseDAO;
import light.head.framework.entity.PageBean;
import light.head.framework.utils.DateUtils;
import light.head.model.AuditStatus;

import org.springframework.stereotype.Service;
/***
 * 审核状态
 * @author yy
 *
 */
@Service
public class AuditStatusDAO extends BaseDAO<AuditStatus, Integer> {
	
	private final static String sql_findByRcMcBusiness = "FROM AuditStatus au WHERE au.rc = ? AND au.mc = ? AND au.business = ?";
	private final static String sql_findByPcMcBusiness = "FROM AuditStatus au WHERE au.pc = ? AND au.mc = ? AND au.business = ?";
	private final static String sql_findByBusinessMcRc = "FROM AuditStatus au WHERE au.business = ? AND au.mc = ? AND au.rc = ?";
	private final static String sql_findByBusinessMc = "FROM AuditStatus au WHERE au.business = ? AND au.mc = ? ORDER BY au.pc";
	private final static String sql_findByRcMcAuBusiness = "FROM AuditStatus item WHERE item.rc = ? AND item.mc = ? AND item.au = ? AND item.business = ?";
	private final static String sql_findAuditStatusBusiness = " FROM AuditStatus a WHERE a.mc = ? AND a.rc = ? AND a.business = ?";
	
	
	public RESULT findByMap(Map<String, String> map, PageBean pb) {
		StringBuffer hql = new StringBuffer("FROM AuditStatus au WHERE 1=1");
		List<Object> list = null;
		if (map != null) {
			list = new ArrayList<Object>();
			String status = map.get(Parm.STATUS);
			String ct = map.get(Parm.CT);
			String cn = map.get(Parm.CN);
			
			if (isNotNull(status)) {
				list.add(Boolean.parseBoolean(status));
				hql.append("AND au.status = ?");
			}
			if (isNotNull(ct)) {
				list.add(DateUtils.getDateByStr(ct, "yyyy-MM-dd"));
				hql.append("AND au.ct >= ?");
			}
			if (isNotNull(ct)) {
				list.add(DateUtils.getDateByStr(ct, "yyyy-MM-dd"));
				hql.append("AND au.ct <= ? ");
			}
			if (isNotNull(cn)) {
				list.add('%' + cn + '%');
				hql.append("AND au.cn LIKE ?");
			}
		}
		hql.append(" ORDER BY au.code DESC");
		return findToPageBean(hql.toString(), pb, list);
	}
	
	
	
	public List<AuditStatus> findByRcMcAuBusiness(Integer rc, Integer mc, Integer au, Byte business) {
		if (null == rc || null == mc || null == au) return null;
		return find(sql_findByRcMcAuBusiness, null, null, rc, mc, au, business);
	}
	
	/**
	 * 根据申请单编号和角色编号查询审核数据
	 */
	public AuditStatus findMcRc(Integer mc, Integer rc,Byte business){
		if (null == rc || null == mc ) return null;
		List<AuditStatus> users = find(sql_findByRcMcBusiness, null, null, rc, mc,business);
		if (null != users && users.size() > 0)
			return users.get(0);
		return null;
	}
	/**
	 * 通过当前节点的业务类型和角色编号获取上一个节点的审核状态
	 * @param business	业务类型
	 * @param rc	角色编号
	 * @param oc	机构编号
	 */
	public Byte checkBeforeByBusinessRc(Byte business, Integer rc, List<Integer> ocs, Integer mc) {
		if (null == business || null == rc || ocs == null || mc == null) return null;
		StringBuffer sql = new StringBuffer("SELECT a.beforeStatus FROM AuditStatus a WHERE a.business = ? AND a.rc = ? AND a.oc IN ( ");
		
		List<Object> parm = new ArrayList<Object>();
		parm.add(business);
		parm.add(rc);
		for (Integer ig : ocs){
			if (ig != null){
				parm.add(ig);
				sql.append("?,");
			}
		}
		sql.deleteCharAt(sql.length() - 1).append(") AND a.mc = ?");
		parm.add(mc);
		
		List<Object> objList = query(sql.toString(), null, 1, parm.toArray());	// 获取审核状态
		
		return (null == objList || objList.size() == 0) ? null : Byte.parseByte(objList.get(0).toString());
	}
	
	/**
	 * 通过角色编号和模块编号查询
	 * @param business		业务编号
	 * @param mc			模块编号
	 */
	public List<AuditStatus> findByRcMcBusiness(Integer rc, Integer mc, Byte business) {
		if (null == rc || null == mc) return null;
		return find(sql_findByRcMcBusiness, null, null, rc,  mc, business);
	}
	
	/**
	 * 获取指定的一个审核节点的状态信息
	 *@param pc		节点序号
	 *@param mc		模块编号
	 *@param business	业务类型编号
	 */
	public List<AuditStatus> findByOcPcMcBusiness(Integer pc, Integer mc, Byte business) {
		if (null == pc || null == mc) return null;
		return find(sql_findByPcMcBusiness, null, null, pc, mc, business);
	}
	
	public Integer ishave(Integer rc, Integer mc, Byte business, List<Integer> ocs){
		if ( null == rc || null == ocs || null == mc) return null;
		StringBuffer sql = new StringBuffer("SELECT au.pc FROM AuditStatus au WHERE au.rc = ? AND au.mc = ? AND au.business = ? AND au.oc IN (");
		
		List<Object> parm = new ArrayList<Object>();
		parm.add(rc);
		parm.add(mc);
		parm.add(business);
		for (Integer ig : ocs){
			if (ig != null){
				parm.add(ig);
				sql.append("?,");
			}
		}
		sql.deleteCharAt(sql.length() - 1).append(")");
		List<Object> objList = query(sql.toString(), null, 1, parm.toArray());	
		return (null == objList || objList.size() == 0) ? null : Integer.parseInt(objList.get(0).toString());
	}
	
	/**
	 * 通过业务类型和模块编号获取审核状态
	 * @param business		业务编号
	 * @param mc			模块编号
	 */
	public List<AuditStatus> findByBusinessMcRc(Byte business, Integer mc, Integer rc) {
		if (null == business || null == mc || null == rc) return null;
		return find(sql_findByBusinessMcRc, null, null, business, mc, rc);
	}
	
	/**
	 * 通过业务类型和模块编号获取审核状态
	 * @param business		业务编号
	 * @param mc			模块编号
	 */
	public List<AuditStatus> findByBusinessMc(Byte business, Integer mc) {
		if (null == business || null == mc) return null;
		return find(sql_findByBusinessMc, null, null, business, mc);
	}
	/**
	 * 通过申请编号和业务类型获取最后一个审核人的状态
	 * @param mc
	 * @param business
	 * @return
	 */
	public Integer getByMcBusiness(Integer mc, Byte business) {
		if (null == business || null == mc) return null;
		StringBuffer sql = new StringBuffer("FROM AuditStatus au WHERE au.business = ? AND au.mc = ? ");
		List<Object> parm = new ArrayList<Object>();
		parm.add(mc);
		parm.add(business);
		sql.append("ORDER BY au.pc DESC");
		List<AuditStatus> objList = find(sql.toString(), null, 1, parm.toArray());	
		return (null == objList || objList.size() == 0) ? null : Integer.parseInt(objList.get(0).getStatus().toString());
	}
	/**
	 * 通过业务类型和模块编号集合获取审核状态
	 * @param business		业务编号
	 * @param mcs			模块编号集合
	 */
	public List<AuditStatus> findByBusinessMc(Byte business, List<Integer> mcs) {
		if (null == business || null == mcs || mcs.size() == 0) return null;
		
		StringBuffer hql = new StringBuffer("FROM AuditStatus au WHERE au.business = ? AND au.mc IN (");
		
		List<Object> parms = new ArrayList<Object>();
		for (Integer i : mcs) {
			if (null != i) {
				parms.add(i);
				hql.append("?,");
			}
		}
		hql.deleteCharAt(hql.length() - 1).append(")");
		return find(hql.toString(), parms.toArray());
	}
	/***
	 * 
	 * @param business
	 * @param oc
	 * @param mc
	 * @return
	 */
	public Integer findByBusinessOcMc(Byte business, Integer oc, Integer mc) {
		if(null == business || null == mc || null == oc) return null;
		StringBuffer sql = new StringBuffer("SELECT a.rc FROM AuditStatus a WHERE a.business = ? AND a.oc = ? AND a.mc = ?");
		
		List<Object> parm = new ArrayList<Object>();
		parm.add(business);
		parm.add(oc);
		parm.add(mc);
		List<Object> objList = query(sql.toString(), null, 1, parm.toArray());	
		//三目表达式
		return (null == objList || objList.size() == 0) ? null : Integer.parseInt(objList.get(0).toString());
	}
	/***
	 * 
	 * @param oc
	 * @param mc
	 * @param pc
	 * @return
	 */
	public Integer findByMcOcPc(Integer oc, Integer mc,Integer pc) {
		if(null == mc || null == oc || null == pc) return null;
		StringBuffer sql = new StringBuffer("SELECT a.rc FROM AuditStatus a WHERE a.oc = ? AND a.mc = ? AND a.pc = ?");
		
		List<Object> parm = new ArrayList<Object>();
		parm.add(oc);
		parm.add(mc);
		parm.add(pc);
		List<Object> objList = query(sql.toString(), null, 1, parm.toArray());	
		return (null == objList || objList.size() == 0) ? null : Integer.parseInt(objList.get(0).toString());
	}
	
	
	/**
	 * 判断 mc 对应的审核状态是否已经有被审核的，有就返回 true， 否则返回 false
	 *@param business	业务类型编号
	 *@param mc			模块编号
	 *@return 有就返回 true， 否则返回 false
	 *@author yangguanghe [2014-6-10_下午4:50:33]
	 */
	public Boolean checkIsAudit(Byte business, Integer mc) {
		if (null == business || null == mc) return null;
		
		List<AuditStatus> list = find(sql_findByBusinessMc,null, 1, business, mc);
		if (null != list && list.size() > 0) {
			for (AuditStatus a : list) {
				if (null != a.getStatus() && a.getStatus().byteValue() == Sys.B_ONE) {
					return true;
				}
			}
		}
		return false;
	}
	
	/**
	 * 判断上一节点的审核状态是否通过, 通过返回 true 否则返回 false
	 *@param business	业务编号
	 *@param mc			模块编号
	 *@param rc			角色编号
	 *@author yangguanghe [2014-6-10_下午6:12:33]
	 */
	public Boolean checkBeforeIsAudit(Byte business, Integer mc, Integer rc) {
		if (null == business || null == mc || null == rc) return null;
		
		String sql = "SELECT a.beforeStatus FROM AuditStatus a WHERE a.business = ? AND a.mc = ? AND a.rc = ?";
		List<Object> obj = query(sql, null, null, business, mc, rc);
		
		if (null != obj && obj.size() > 0) {
			if (null == obj.get(0))
				return false;
			if (Sys.B_ONE == Byte.parseByte(obj.get(0).toString()))
				return true;
		}
		
		return false;
	}
	
	/***
	 * 
	 * @param mc
	 * @param rc
	 * @return
	 */
	public List<AuditStatus> findAuditStatusBusiness(Integer mc, Integer rc, Byte business) {
		if (null == mc || null == rc)return null;
		return find(sql_findAuditStatusBusiness, null, null, mc, rc, business);
	}
	
	/**
	 * 根据申请单编号和角色编号和审核序号查询上一级审核状态是否为未审核
	 */
	public AuditStatus findMcPc(Integer pc, Integer mc,Byte business){
		if (null == pc || null == mc ) return null;
		List<AuditStatus> users = find(sql_findByPcMcBusiness, null, null, pc, mc, business);
		if (null != users && users.size() > 0)
			return users.get(0);
		return null;
	}
	
}
